const {Sequelize, Model} = require('sequelize')
const {sequelize} = require('../../core/db')
const axios  = require('axios')
const util = require('util')
const { Favor } = require('./favor.js')
class Book extends Model {
  // constructor(id) {
  //   super()
  //   this.id = id
  // }
  async detail(id) {
     const url = util.format(global.config.yushu.detailUrl, id)
     const detail = await axios.get(url)
     return detail.data
  }

  static async serachFromYuShu(q, start, count, summary=1) {
    const url = util.format(global.config.yushu.keywordUrl, encodeURI(q), start, count, summary)
    const result = await axios.get(url)
    return result.data
  }

  static async getMyFavorBookCount(uid) {
    const count = await Favor.count({
        where: {
            type: 400,
            uid
        }
    })
    return count
  }
}

Book.init({
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true
  },
  fav_nums: {
    type: Sequelize.INTEGER,
    defaultValue: 0
  }
}, {
  sequelize,
  tableName: 'book'
})

module.exports = {
  Book
}